<template>
	<view class="content">
		<view class="content_shang">
			<view class="shang_shang">
				<view class="shang_left">
					货源
				</view>
				<view class="shang_right">
					<image src="../../static/sourceofgoods/shuaxuan.png"></image>
				</view>
			</view>
			<view class="shang_xia">
				<view :class="{ selected: activeButton === '4' }" @click="handleClick('4')">
					全部
				</view>
				<view :class="{ selected: activeButton === '0' }" @click="handleClick('0')">
					待充值
				</view>
				<view :class="{ selected: activeButton === '1' }" @click="handleClick('1')">
					进行中
				</view>
				<view :class="{ selected: activeButton === '2' }" @click="handleClick('2')">
					已结束
				</view>
				<view :class="{ selected: activeButton === '3' }" @click="handleClick('3')">
					已暂停
				</view>
			</view>
		</view>
		<view class="content_xia">
			<view class="shang_sousuo">
				<uni-search-bar placeholder="货源ID查询" cancelButton="none" v-model="input" @confirm="onSearch"
					@input="onInput"></uni-search-bar>
			</view>

			<!-- 如果货源数据为空，则显示提示 -->
			<view v-if="du==0" class="empty-data">
				<view class="icon">
					<image src="../../static/sourceofgoods/zanwu.png" alt="暂无数据"></image>
				</view>
				<text>暂无货源数据</text>
			</view>
			<view v-else class="huowu" v-for="opt in goodsData">
				<view class="hw_shang">
					<view class="hw_one">
						<view class="one_left">
							<view class="yuan">
								装
							</view>
							{{opt.shippingaddress}}
						</view>
						<text
							:class="'goods-status ' +getStatusClass(opt.status)">{{ getStatusText(opt.status) }}</text>
					</view>
					<view class="hw_two">
						<view class="yuan">
							卸
						</view>
						<view class="two_text">
							{{opt.unloadingaddress}}
						</view>
					</view>
				</view>
				<view class="hw_xia">
					<view>
						<image src="../../static/sourceofgoods/huowu.png"></image>
						<text>货物：</text>
						<text>{{opt.cargoname}}</text>
						<text class="ss">运价：</text>
						<text>{{opt.unitprice}}元/吨</text>
					</view>
					<view>
						<image src="../../static/sourceofgoods/yizhuang.png"></image>
						<text>已装：</text>
						<text>{{chushi}}/{{opt.totalweight}}吨</text>
					</view>
					<view>
						<image src="../../static/sourceofgoods/fahuoshijian.png"></image>
						<text>最晚发货：</text>
						<text>{{opt.starttime}}</text>

					</view>
					<view>
						<image src="../../static/sourceofgoods/id.png"></image>
						<text>{{opt.sourceid}}</text>

					</view>
				</view>
				<view class="anniu">
					<view class="anniu_o" @click="xiangqing(opt)">
						货源统计
					</view>
					<view class="anniu_o">
						运单记录
					</view>
					<view class="anniu_p">
						再次发布
					</view>
					<view class="anniu_p">
						装车/卸车
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		http
	} from '@/config/request.js'
	import "@/config/request.js";
	import {
		useuser
	} from '../../stores/user';
	const user = useuser();
	const goodsData = ref([]);
	const chushi = ref(0.0);
	const du = ref(0);
	const input = ref('');
	// 点击搜索按钮
	const onInput = (e) => {
		console.log(e)
		input.value = e;
	};
	//搜索
	const onSearch = async() => {
		const query = input.value.trim();
		if (!query) {
			chakan()
			return;
		}
		// 这里你可以加搜索逻辑，比如调用后端接口进行查询
		console.log('搜索货源ID：', query);
		// TODO：筛选 goodsData 或请求后端接口
		const res =await http({
			url: '/selectHuoYuanw/' + query,
			method: 'GET'
		})
		
		if (res !=null) {
			goodsData.value = [res.shuju]; // 如果是单条数据，用数组包裹
		} else {
			uni.showToast({
				title: '未找到该货源',
				icon: 'none'
			});
			goodsData.value = [];
		}
	}
	// 获取货源状态的文本
	const getStatusText = (status) => {
		switch (status) {
			case 0:
				return '待充值';
			case 1:
				return '进行中';
			case 2:
				return '已结束';
			case 3:
				return '已暂停';
			default:
				return '待开始';
		}
	}
	const xiangqing = (opt) => {
		console.log(opt)
		//带参跳转
		uni.navigateTo({
			url: '/pages/sourceofgoods/pageinfo/sourcedetails?sourceid=' + opt.sourceid
		})
	}
	// 获取状态的类名（用于设置样式）
	const getStatusClass = (status) => {
		switch (status) {
			case 0:
				return 'pending'; // 待充值
			case 1:
				return 'in-progress'; // 进行中
			case 2:
				return 'completed'; // 已结束
			case 3:
				return 'paused'; // 已暂停
			default:
				return 'not-started'; // 待开始
		}
	}
	const activeButton = ref(''); // 记录选中的按钮
	const cha = async (duixiang) => {
		const res = await http({
			url: '/selectHuoYuanBYStatus',
			method: 'post',
			data: duixiang
		})
		// 检查 res.shuju 是否为有效数组
		if (res.shuju && Array.isArray(res.shuju)) {
			du.value = res.shuju.length; // 获取数组的长度
			goodsData.value = res.shuju; // 更新 goodsData
			goodsData.value.forEach(item => {
				chushi.value = item.totalweight - item.totalweights;
			});
		} else {
			du.value = 0; // 如果 res.shuju 无效（如 undefined 或 null），设置 du 为 0
			goodsData.value = []; // 确保 goodsData 是一个空数组
		}

		console.log(goodsData.value)
	}
	const chakan = async () => {
		const res = await http({
			url: '/selectHuoYuanq/' + user.user.id,
			method: 'GET'
		})
		console.log(res)

		// 检查 res.shuju 是否为有效数组
		if (res.shuju && Array.isArray(res.shuju)) {
			du.value = res.shuju.length; // 获取数组的长度
			goodsData.value = res.shuju; // 更新 goodsData
		} else {
			du.value = 0; // 如果 res.shuju 无效，设置 du 为 0
			goodsData.value = []; // 确保 goodsData 是一个空数组
		}
		console.log(goodsData.value)
	}

	// 点击事件
	const handleClick = (buttonName) => {
		activeButton.value = buttonName; // 更新选中的按钮
		input.value = '';
		console.log(activeButton.value)
		if (buttonName != '4') {
			var duixiang = {
				driverid: user.user.id,
				status: buttonName
			}
			cha(duixiang);
		} else {
			chakan();
		}
	};
	onLoad(() => {
		chakan();
		activeButton.value = '4'; // 默认选中“全部”按钮
	})
</script>

<style lang="scss">
	/* 各个状态的颜色 */
	.goods-status {
		font-weight: bold;
		padding: 5px 10px;
		border-radius: 5px;
		text-align: center;
	}

	.goods-status.pending {

		color: #ff0000;
	}

	.goods-status.in-progress {
		color: #55ff7f;
	}

	.goods-status.completed {

		color: #ff0000;
	}

	.goods-status.paused {

		color: #7f8c8d;
	}

	.goods-status.not-started {

		color: #7f8c8d;
	}


	.empty-data {
		width: 100%;
		height: 400rpx;
		/* 高度可以根据实际情况调整 */
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		background-color: #f8f8f8;
		border-radius: 15rpx;
		border: 1px solid #dcdcdc;
		/* 边框颜色 */
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.1);
		/* 轻微的阴影 */
		margin-top: 20rpx;
		text-align: center;
	}

	.empty-data text {
		color: #888888;
		font-size: 28rpx;
		font-weight: 500;
		line-height: 40rpx;
	}

	.empty-data .icon {
		width: 100rpx;
		height: 100rpx;
		background-color: #f0f0f0;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 15rpx;
	}

	.empty-data .icon image {
		width: 100rpx;
		height: 100rpx;
	}


	.content {
		background: #0000ff;

		.content_shang {
			width: 100%;
			height: 200rpx;
			background-color: #002fa7;
			color: #fff;

			.shang_shang {
				width: 100%;
				height: 100rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;


				.shang_left {
					margin-left: 20rpx;
					font-size: 50rpx;
				}

				.shang_right {
					margin-right: 20rpx;
				}

				.shang_right image {
					width: 40rpx;
					height: 40rpx;

				}
			}

			.shang_xia {
				width: 100%;
				height: 100rpx;
				display: flex;
				justify-content: space-around;
				align-items: center;

				view {
					font-size: 30rpx;
					width: 100rpx;
					text-align: center;
					padding-bottom: 15rpx;
					color: #d5d5d5;
				}

			}
		}

		.content_xia {
			width: 100%;
			background-color: #eeeeee;
			border-radius: 20rpx 20rpx 0 0;
			display: flex;
			flex-direction: column;
			align-items: center;
			min-height: 100vh;
			/* 让底部内容的高度至少是屏幕的高度 */
			padding-bottom: 20rpx;
			/* 适当添加一些底部空间 */


			.shang_sousuo {
				width: 100%;
				height: 100rpx;

				uni-search-bar {
					width: 100%;
					height: 100rpx;
					background-color: #ffffff;
					border-radius: 50rpx;
					margin: 0 auto;
					padding: 0;

				}
			}

			.huowu {
				width: 90%;
				height: 420rpx;
				display: flex;
				background-color: #fff;
				border-radius: 20rpx;
				margin-top: 20rpx;
				flex-wrap: wrap;
				margin-bottom: 20rpx;
				justify-content: space-around;


				.hw_shang {

					width: 100%;
					height: 100rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					margin: 30rpx;
					font-size: 27rpx;

					.hw_one {
						width: 100%;
						display: flex;
						justify-content: space-between;
						align-items: center;
						margin: 10rpx;


						.one_left {
							display: flex;
							align-items: center;

							.yuan {
								width: 40rpx;
								height: 40rpx;
								background-color: #002fa7;
								border-radius: 50%;
								text-align: center;
								line-height: 40rpx;
								margin-right: 20rpx;
								font-size: 20rpx;
								color: #fff;
							}
						}

						.one_right {
							color: #ff3939;
							font-weight: bold;
						}
					}

					.hw_two {
						display: flex;
						margin: 10rpx;
						align-items: center;

						.yuan {
							width: 40rpx;
							height: 40rpx;
							background-color: #ff3939;
							border-radius: 50%;
							text-align: center;
							line-height: 40rpx;
							margin-right: 20rpx;
							font-size: 20rpx;
							color: #fff;
						}
					}

				}

				.hw_xia {
					width: 100%;
					height: 210rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					margin-left: 40rpx;

					margin-top: -40rpx;
					font-size: 25rpx;

					.ss {
						margin-left: 100rpx;
					}
				}

				.anniu {
					display: flex;
					height: 50rpx;
					justify-content: space-around;
					padding-left: 50rpx;
					padding-bottom: 10rpx;

					.anniu_o {
						width: 110rpx;
						height: 50rpx;
						background-color: #fff;
						border-radius: 10rpx;
						text-align: center;
						line-height: 50rpx;
						color: #909090;
						font-size: 25rpx;
						border: 1px solid #ababab;
					}

					.anniu_p {
						width: 110rpx;
						height: 50rpx;
						background-color: #edfcff;
						text-align: center;
						line-height: 50rpx;
						border-radius: 10rpx;
						color: #5555ff;
						font-size: 25rpx;
						border: 1px solid #ababab;
					}
				}
			}
		}
	}

	.anniu view {
		margin-right: 15rpx;
	}

	.hw_xia image {
		width: 30rpx;
		height: 30rpx;
		margin-right: 20rpx;
		margin-left: 20rpx;
	}

	.hw_xia view {
		display: flex;
		align-items: center;
		margin-top: 20rpx;

	}

	.shang_xia view.selected {
		color: #002fa7;
		/* 选中后的字体颜色 */
		border-bottom: 4rpx solid #fff;
		/* 选中时的下边框 */
	}
</style>